import { _decorator, Component, Node,  js, Button, warn, EventHandler, isValid, find, Skeleton, SpriteFrame, Sprite, Label, ProgressBar, EditBox, Slider } from 'cc';
import GameEvent from '../event/GameEvent';
import Utils from '../utils/Utils';
import { ComponentEx } from './ComponentEx';

export class BaseUI extends ComponentEx {
    onBtnClicked(event:any, customEventData:any) {
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.close();
                break;
        }
    }

    close()
    {

    }
    
    events:any[] = [];

    onLoad() {
        this.events = [];
        this._addClickEvent(this.node);
    }

    onDestroy() {
        for (var i = 0; i < this.events.length; ++i)
            GameEvent.Instance().unregister(this, this.events[i]);
    }

    register(type: any, callFunc: Function) {
        this.events.push(type);
        GameEvent.Instance().register(this, type, callFunc);
    }
    unregister(type: any) {
        GameEvent.Instance().unregister(this, type);
    }

    dispatch(type: any, ...data: any[]) {
        GameEvent.Instance().dispatch(type, ...data);
    }

    shutAnim() {
        if (this.node) {
            this.node.active = false;
            this.node.parent = null;
            this.node.destroy();
        }
    }
    
    _isSkipNode(node: Node): boolean {
        if (this.node == node) {
            return false;
        }
        let b = node.getComponent(BaseUI);
        if (b)  console.log("跳过", b.name)
        return b != null;
    }

    _addClickEvent(node: Node) {
        if (this._isSkipNode(node)) return;
        for (var i = 0; i < node.children.length; ++i) {
            var tmp = node.children[i];
            if (this._isSkipNode(tmp)) continue;
            if (tmp.getComponent(Button)) {
                BaseUI.addClickEvent(tmp, this.node, js.getClassName(this), "onBtnClicked", tmp);
            }
            this._addClickEvent(tmp);
        }
    }

    public static addClickEvent(node, target, component, handler, customEventData) {
        var eventHandler = new EventHandler()
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;               
        if (customEventData) eventHandler.customEventData = customEventData;

        var clickEvents = node.getComponent(Button).clickEvents;
        if (clickEvents.length > 0) {
            warn("按钮已经存在绑定，跳过自动绑定", node.name);
            return;
        }
        clickEvents.push(eventHandler);
    }


    
    // public SetSpriteUrl(name: string, url: string) {
    //     if (!window['wx'] && !window['tt'] && !window['qq']) return;

    //     if (url && url != "") {
    //         var tmp = this.GetGameObject(name);
    //         if (tmp) {
    //             loader.load({ url, type: 'jpg' }, (err, tex) => {
    //                 if (!err) {
    //                     if (!isValid(tmp)) return;
    //                     tmp.getComponent(Sprite).spriteFrame = new SpriteFrame(tex);
    //                 }
    //             });
    //         }
    //     }
    //     else {
    //         if (this.GetSprite(name))
    //             this.GetSprite(name).spriteFrame = null;
    //     }
    // }


    public GetGameObject(name: string, refind: boolean = false): Node {
        if (!isValid(this.node)) return null;
        if (!refind) {
            if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name];
            if (name == this.node.name) return this.node;
        }

        if (name.indexOf("/") != -1) {
            var tmp = find(name, this.node);
            if (tmp) this.m_objects[name] = tmp;
            return tmp;
        }
        else {
            var tmp = this._findInChildren(this.node, name);
            if (tmp) this.m_objects[name] = tmp;
            return tmp;
        }
    }

    // public GetSkeleton(name: string): Skeleton {
    //     if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(Skeleton);
    //     var tmp = this.GetGameObject(name);
    //     if (tmp) return tmp.getComponent(Skeleton);
    //     return null;
    // }

    public GetSprite(name: string): Sprite {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(Sprite);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(Sprite);
        return null;
    }



    public GetText(name: string): Label {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(Label);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(Label);
        return null;
    }

    public GetProgressBar(name: string): ProgressBar {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(ProgressBar);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(ProgressBar);
        return null;
    }

    public GetButton(name: string): Button {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(Button);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(Button);
        return null;
    }

    public GetInputField(name: string): EditBox {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(EditBox);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(EditBox);
        return null;
    }

    public GetSlider(name: string): Slider {
        if (this.m_objects && this.m_objects.has(name)) return this.m_objects[name].getComponent(Slider);
        var tmp = this.GetGameObject(name);
        if (tmp) return tmp.getComponent(Slider);
        return null;
    }

    public SetText(TextID: string, content: string) {
        if (this.GetText(TextID))
            this.GetText(TextID).string = content;
    }

    public SetInputText(TextID: string, content: string) {
        if (this.GetInputField(TextID))
            this.GetInputField(TextID).string = content;
    }

    public SetProgressBar(TextID: string, p: number) {
        if (this.GetProgressBar(TextID))
            this.GetProgressBar(TextID).progress = p;
    }

}
